# Copyright (c) 2023, Arm Limited and Contributors. All rights reserved.
# SPDX-License-Identifier: Apache-2.0

if(NOT CMAKE_CROSSCOMPILING)
    return()
endif()

if(NOT CMAKE_C_COMPILER_ID STREQUAL "GNU")
    return()
endif()

add_library(iotsdk-sanitizers-asan asan.c)
target_include_directories(iotsdk-sanitizers-asan
    PUBLIC
        include
)
target_link_libraries(iotsdk-sanitizers-asan
    PUBLIC
        iotsdk-sanitizers-common
)
target_compile_options(iotsdk-sanitizers-asan
    INTERFACE
        -fsanitize=kernel-address
        -g
        -Og
)
target_link_options(iotsdk-sanitizers-asan
    INTERFACE
        -Wl,--wrap=malloc
        -Wl,--wrap=free
)
target_compile_definitions(iotsdk-sanitizers-asan
    PUBLIC
        IOTSDK_ASAN_WRAP_MALLOC=__wrap_malloc
        IOTSDK_ASAN_WRAP_FREE=__wrap_free
    PRIVATE
        IOTSDK_ASAN_REAL_MALLOC=__real_malloc
        IOTSDK_ASAN_REAL_FREE=__real_free
)
